package net.hctek.AnnouncerStation.action;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;

import net.hctek.AnnouncerStation.service.IdentityServiceI;
import net.hctek.AnnouncerStation.util.ChineseUtill;
/** 
* @author Ken 384755534@qq.com: 
* @version 创建时间：2017年2月21日 上午9:45:53 
* 类说明 :文件下载
*/ 
public class FileDownloadAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private String filename;
	private String downloadFileName;
	
	@Autowired
	private IdentityServiceI identityServiceI;
	
	public 	String execute(){
		return SUCCESS;
	}
	
	public InputStream getInputStream(){
/*		//获取用户信息
		User u = (User)ServletActionContext.getRequest().getSession().getAttribute(Contant.SESSION_USER);*/
		//判断传入值是否乱
		if(ChineseUtill.isMessyCode(filename)){
			setFilename(getFilename());
			System.out.println("true"+filename);
		}else{
			System.out.println("false"+filename);
		}
		//获取文件路径
		String filePath = ServletActionContext.getServletContext().getRealPath("/upload/"+"Musiclibrary");
		//拼接完整路径
		String FilePath = filePath+"/"+filename;
		System.out.println("FilePath:"+FilePath);
		//根据完整路径读取文件
		File file = new File(FilePath);
		try {
			SimpleDateFormat bartDateFormat = new SimpleDateFormat
	  				("yyyy-MM-dd"); 
			Date date = new Date(); 
			Long i = 1L;
			identityServiceI.updataDownloadCount(i,bartDateFormat.format(date));
			return  FileUtils.openInputStream(file);
		} catch (Exception e1) {
			e1.printStackTrace();
			System.out.println("getInputStream:"+"出异常");
		}
		return null;
	}
	
	public String getDownloadFileName()  {
		 HttpServletRequest request = ServletActionContext.getRequest();
		String downFileName = filename;
		try {
			/*downFileName = new String(downFileName.getBytes("UTF-8"),"ISO8859-1");*/
			if (request.getHeader("User-Agent").toLowerCase()  
	                .indexOf("firefox") > 0) {  
				downFileName = new String(downFileName.getBytes("UTF-8"), "ISO8859-1"); // firefox浏览器  
	        } else if (request.getHeader("User-Agent").toUpperCase()  
	                .indexOf("MSIE") > 0) {  
	        	downFileName = URLEncoder.encode(downFileName, "UTF-8");// IE浏览器  
	        	downFileName = downFileName.replace("+", "%20");
	        }else if (request.getHeader("User-Agent").toUpperCase()  
	                .indexOf("CHROME") > 0) {  
	        	downFileName = new String(downFileName.getBytes("UTF-8"), "ISO8859-1");// 谷歌  
	        }
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} 
		return downFileName;
	}

	public void setDownloadFileName(String downloadFileName) {
		this.downloadFileName = downloadFileName;
	}

	public String getFilename() {
		try {
			//将文件名转码。
			filename = new String(filename.getBytes("ISO-8859-1"),"UTF-8");
			return filename;
		} catch (Exception e) {
			System.out.println("转码失败2");
			e.printStackTrace();
		}
		return null;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}
	
}
